# Create your views here.
from django.db.models import Count
from django.http import JsonResponse

from risk.models import *
from risk.serializers import *
from dvadmin.utils.viewset import CustomModelViewSet
from datetime import datetime, timedelta

class TousujvbaoViewSet(CustomModelViewSet):
    """
    list:查询
    create:新增
    update:修改
    retrieve:单例
    destroy:删除
    """
    queryset = Tousujvbao.objects.all()
    serializer_class = TousujvbaoSerializer
    create_serializer_class = TousujvbaoCreateUpdateSerializer
    update_serializer_class = TousujvbaoCreateUpdateSerializer
    filter_fields = ['辖区', '主体名称', '类型', '状态', ]
    search_fields = ['主体名称', '类型', '状态', '辖区']




def get_present_data(request, *args, **kwargs):
    response = {"data": {}}

    response["data"]["type"] = (Tousujvbao.objects.filter(状态="已办结").values("辖区", "类型")
                                .annotate(count=Count("id")))

    response |= {
        "code": 2000,
        "msg": "success"
    }

    return JsonResponse(response)
